-
Notifications
You must be signed in to change notification settings - Fork 0
/
importScript.js
59 lines (40 loc) · 1.2 KB
/
importScript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import fs from 'fs';
import path from 'path';
import webpack from 'webpack';
import to from 'await-to-js';
import compile from './compile.js';
export default async function( filepath, functionNames ) {
// First let's create file to bundle.
let name = path.basename( filepath ).replace( '.js', '' );
let entry = filepath;
if( functionNames ) {
for( let functionName of functionNames ) {
name = `${name}.${functionName}`;
}
entry = `./.edge/__${name}.js`;
let functionImports = [];
for( let name of functionNames ) {
functionImports.push( `${name} as __${name}` );
}
functionImports = functionImports.join();
const fileContent = `
import { ${functionImports} } from '../${filepath}';
${functionNames.map( name => `window.${name} = __${name};` )}
`;
fs.writeFileSync( entry, fileContent );
}
const config = {
mode: process.env.NODE_ENV,
devtool: false,
entry: {
[name]: entry,
},
output: {
path: path.resolve( __dirname, '.edge' ),
filename: '[name].js',
}
};
await compile( config, name );
const fileContents = fs.readFileSync( `./.edge/${name}.js`, 'utf-8' );
return fileContents;
}