Skip to content

Commit 962bac5

Browse files
authored
Support for Vue 3 (codesandbox#1059)
1 parent e250a07 commit 962bac5

File tree

8 files changed

+133368
-28
lines changed

8 files changed

+133368
-28
lines changed

packages/app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
"@cerebral/http": "^4.0.0",
102102
"@cerebral/mobx-state-tree": "^3.0.0",
103103
"@emmetio/codemirror-plugin": "^0.3.5",
104+
"@vue/babel-preset-app": "^3.0.1",
104105
"apollo-boost": "^0.1.7",
105106
"apollo-link-batch-http": "^1.2.2",
106107
"apollo-link-context": "^1.0.8",

packages/app/src/sandbox/eval/transpilers/babel/babel-parser.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ const DEFAULT_BABEL_CONFIG = {
3232
export default function getBabelConfig(
3333
config: ?Object,
3434
loaderOptions: Object,
35-
path: string
35+
path: string,
36+
isV7: boolean = false
3637
) {
3738
const resolvedConfig = config || DEFAULT_BABEL_CONFIG;
3839

@@ -47,17 +48,16 @@ export default function getBabelConfig(
4748
filename: path,
4849
};
4950

51+
const commonjsPluginName = isV7
52+
? 'transform-modules-commonjs'
53+
: 'transform-es2015-modules-commonjs';
54+
5055
if (finalConfig.plugins) {
51-
if (
52-
finalConfig.plugins.indexOf('transform-es2015-modules-commonjs') === -1
53-
) {
54-
finalConfig.plugins = [
55-
...finalConfig.plugins,
56-
'transform-es2015-modules-commonjs',
57-
];
56+
if (finalConfig.plugins.indexOf(commonjsPluginName) === -1) {
57+
finalConfig.plugins = [...finalConfig.plugins, commonjsPluginName];
5858
}
5959
} else {
60-
finalConfig.plugins = ['transform-es2015-modules-commonjs'];
60+
finalConfig.plugins = [commonjsPluginName];
6161
}
6262

6363
return finalConfig;

packages/app/src/sandbox/eval/transpilers/babel/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,20 @@ class BabelTranspiler extends WorkerTranspiler {
6767

6868
const loaderOptions = loaderContext.options || {};
6969

70-
const babelConfig = getBabelConfig(foundConfig, loaderOptions, path);
71-
7270
const isV7 = !!(
7371
configs.package &&
7472
configs.package.parsed &&
7573
configs.package.parsed.devDependencies &&
7674
configs.package.parsed.devDependencies['@vue/cli-plugin-babel']
7775
);
7876

77+
const babelConfig = getBabelConfig(
78+
foundConfig,
79+
loaderOptions,
80+
path,
81+
isV7
82+
);
83+
7984
this.queueTask(
8085
{
8186
code,

packages/app/src/sandbox/eval/transpilers/babel/worker/babel-worker.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ self.addEventListener('message', async event => {
249249
}
250250

251251
const stringifiedConfig = JSON.stringify(babelTranspilerOptions);
252-
if (lastConfig !== stringifiedConfig) {
252+
if (stringifiedConfig && lastConfig !== stringifiedConfig) {
253253
resetCache();
254254
lastConfig = stringifiedConfig;
255255
}
@@ -274,6 +274,19 @@ self.addEventListener('message', async event => {
274274
Babel.registerPreset('env', Babel.availablePresets.es2015);
275275
}
276276

277+
// Future vue preset
278+
// if (
279+
// flattenedPresets.indexOf('@vue/app') > -1 &&
280+
// Object.keys(Babel.availablePresets).indexOf('@vue/app') === -1 &&
281+
// version === 7
282+
// ) {
283+
// const vuePreset = await import(/* webpackChunkName: 'babel-preset-vue' */ '@vue/babel-preset-app');
284+
285+
// Babel.registerPreset('@vue/app', vuePreset);
286+
// Babel.registerPreset('@vue/babel-preset-app', vuePreset);
287+
288+
// }
289+
277290
if (
278291
flattenedPlugins.indexOf('transform-vue-jsx') > -1 &&
279292
Object.keys(Babel.availablePlugins).indexOf('transform-vue-jsx') === -1

packages/app/src/sandbox/eval/transpilers/babel/worker/evaluate.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import resolve from 'browser-resolve';
22
import hashsum from 'hash-sum';
3+
import { dirname } from 'path';
34
import type FSType from 'fs';
45
import evaluateCode from '../../../loaders/eval';
56

67
let cache = {};
8+
let cachedPaths = {};
79
let transpileBeforeExec = false;
810

911
export const resetCache = () => {
1012
cache = {};
13+
cachedPaths = {};
1114
transpileBeforeExec = false;
1215
};
1316

@@ -63,17 +66,24 @@ export default function evaluate(
6366
return preset;
6467
}
6568

66-
const resolvedPath = resolve.sync(requirePath, {
67-
filename: path,
68-
extensions: ['.js', '.json'],
69-
moduleDirectory: ['node_modules'],
70-
});
69+
const dirName = dirname(path);
70+
cachedPaths[dirName] = cachedPaths[dirName] || {};
71+
72+
const resolvedPath =
73+
cachedPaths[dirName][requirePath] ||
74+
resolve.sync(requirePath, {
75+
filename: path,
76+
extensions: ['.js', '.json'],
77+
moduleDirectory: ['node_modules'],
78+
});
79+
80+
cachedPaths[dirName][requirePath] = resolvedPath;
7181

7282
let resolvedCode = fs.readFileSync(resolvedPath).toString();
7383
const id = hashsum(resolvedCode + resolvedPath);
7484

7585
if (cache[id]) {
76-
return cache[id];
86+
return cache[id].exports;
7787
}
7888

7989
cache[id] = {};
@@ -84,16 +94,14 @@ export default function evaluate(
8494
resolvedCode = transpiledCode;
8595
}
8696

87-
cache[id] = evaluate(
97+
return evaluate(
8898
fs,
8999
BFSRequire,
90100
resolvedCode,
91101
resolvedPath,
92102
availablePlugins,
93103
availablePresets
94104
);
95-
96-
return cache[id];
97105
};
98106

99107
// require.resolve is often used in .babelrc configs to resolve the correct plugin path,
@@ -106,7 +114,8 @@ export default function evaluate(
106114
// moduleDirectory: ['node_modules'],
107115
// });
108116

109-
const module = {
117+
const id = hashsum(code + path);
118+
cache[id] = {
110119
id: path,
111120
exports: {},
112121
};
@@ -117,11 +126,11 @@ export default function evaluate(
117126
}
118127
finalCode += `\n//# sourceURL=${location.origin}${path}`;
119128

120-
evaluateCode(finalCode, require, module, {
129+
const exports = evaluateCode(finalCode, require, cache[id], {
121130
VUE_CLI_BABEL_TRANSPILE_MODULES: true,
122131
});
123132

124-
return module.exports;
133+
return exports;
125134
}
126135

127136
export function evaluateFromPath(

0 commit comments

Comments
 (0)