Skip to content

Commit

Permalink
feat: basic support for dynamic imports
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 22, 2020
1 parent 7838c5e commit 9494452
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@types/mkdirp": "latest",
"@types/node": "latest",
"@types/resolve": "latest",
"babel-plugin-dynamic-import-node": "latest",
"create-require": "latest",
"eslint": "latest",
"esm": "latest",
Expand Down
3 changes: 2 additions & 1 deletion src/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export default function transform (opts: TransformOptions): string {
filename: '',
cwd: '/',
plugins: [
[require('@babel/plugin-transform-modules-commonjs'), { allowTopLevelThis: true }]
[require('@babel/plugin-transform-modules-commonjs'), { allowTopLevelThis: true }],
[require('babel-plugin-dynamic-import-node')]
]
}

Expand Down
6 changes: 4 additions & 2 deletions src/jiti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const defaults = {
cache: true
}

const TRANSPILE_VERSION = 1.1

function md5 (content: string, len = 8) {
return createHash('md5').update(content).digest('hex').substr(0, len)
}
Expand Down Expand Up @@ -67,7 +69,7 @@ export default function createJITI (_filename: string = process.cwd(), opts: JIT
}

// Calculate source hash
const sourceHash = ` /* v1-${md5(source, 16)} */`
const sourceHash = ` /* v${TRANSPILE_VERSION}-${md5(source, 16)} */`

// Check cache file
const filebase = basename(dirname(filename)) + '-' + basename(filename)
Expand Down Expand Up @@ -109,7 +111,7 @@ export default function createJITI (_filename: string = process.cwd(), opts: JIT
if (filename.match(/\.ts$/)) {
debug('[ts]', filename)
source = getCache(filename, source, () => opts.transform!({ source, filename, ts: true }))
} else if (source.match(/^\s*import .* from/m) || source.match(/^\s*export /m)) {
} else if (source.match(/^\s*import .* from/m) || source.match(/import\s*\(/) || source.match(/^\s*export /m)) {
debug('[esm]', filename)
source = getCache(filename, source, () => opts.transform!({ source, filename }))
} else {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/esm/dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const A = () => import('./index')
A().then(console.log)
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ babel-eslint@^10.1.0:
eslint-visitor-keys "^1.0.0"
resolve "^1.12.0"

babel-plugin-dynamic-import-node@^2.3.3:
babel-plugin-dynamic-import-node@^2.3.3, babel-plugin-dynamic-import-node@latest:
version "2.3.3"
resolved "https://registry.yarnpkg.com/babel-plugin-dynamic-import-node/-/babel-plugin-dynamic-import-node-2.3.3.tgz#84fda19c976ec5c6defef57f9427b3def66e17a3"
integrity sha512-jZVI+s9Zg3IqA/kdi0i6UDCybUI3aSBLnglhYbSSjKlV7yF1F/5LWv8MakQmvYpnbJDS6fcBL2KzHSxNCMtWSQ==
Expand Down

0 comments on commit 9494452

Please sign in to comment.