Skip to content

Commit c7f5115

Browse files
author
Ives van Hoorne
committed
Allow resolving of /@/
1 parent e9f1dac commit c7f5115

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

src/common/sandbox/resolve-module.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@ const throwError = (path: string) => {
1616
};
1717

1818
export function getModulesInDirectory(
19-
path: ?string,
19+
_path: ?string,
2020
modules: Array<Module>,
2121
directories: Array<Directory>,
22-
startdirectoryShortid: ?string = undefined
22+
_startdirectoryShortid: ?string = undefined
2323
) {
24-
if (!path) return throwError('');
24+
if (!_path) return throwError('');
25+
26+
let path = _path;
27+
let startdirectoryShortid = _startdirectoryShortid;
28+
// If paths start with {{sandboxRoot}} we see them as root paths
29+
if (path.startsWith('{{sandboxRoot}}')) {
30+
startdirectoryShortid = undefined;
31+
path = _path.replace('{{sandboxRoot}}/', './');
32+
}
2533

2634
// Split path
2735
const splitPath = path.replace(/^.\//, '').split('/');

src/sandbox/eval/presets/vue-cli/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import base64Transpiler from '../../transpilers/base64';
1515

1616
import Preset from '../';
1717

18-
const vuePreset = new Preset('vue-cli', ['vue', 'json', 'js']);
18+
const vuePreset = new Preset('vue-cli', ['vue', 'json', 'js'], {
19+
'@': '{{sandboxRoot}}',
20+
});
1921

2022
const sassWithConfig = {
2123
transpiler: sassTranspiler,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ self.addEventListener('message', async event => {
5858
const dependencies = getDependencies(result.ast);
5959

6060
dependencies.forEach(dependency => {
61-
if (/^(\w|@)/.test(dependency.path) && !dependency.path.includes('!')) {
61+
if (/^(\w|@\w)/.test(dependency.path) && !dependency.path.includes('!')) {
6262
// Don't push dependencies
6363
return;
6464
}

src/sandbox/eval/transpilers/css/get-modules.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const core = new Core();
77
export default (code: string, loaderContext: LoaderContext) =>
88
core
99
.load(code, loaderContext.path, (dependencyPath: string) => {
10+
console.log(dependencyPath);
1011
const tModule = loaderContext.addDependency(dependencyPath);
1112

1213
return tModule.source ? tModule.source.compiledCode : tModule.module.code;

src/sandbox/eval/transpilers/vue/loader.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export default function(code: string, loaderContext: LoaderContext) {
8484
parts.styles.forEach(function(style, i) {
8585
// require style
8686
const requireLocation = style.src
87-
? style.src
87+
? getRequireForImport('style', style, i, style.scoped)
8888
: getRequire('style', style, i, style.scoped);
8989

9090
const requireString = `require('${requireLocation}')`;
@@ -127,7 +127,9 @@ export default function(code: string, loaderContext: LoaderContext) {
127127
output += ' /* script */\n ';
128128
var script = parts.script;
129129
if (script) {
130-
const file = script.src ? script.src : getRequire('script', script);
130+
const file = script.src
131+
? getRequireForImport('script', script)
132+
: getRequire('script', script);
131133

132134
output += `require('${file}')`;
133135
} else {
@@ -140,7 +142,7 @@ export default function(code: string, loaderContext: LoaderContext) {
140142
var template = parts.template;
141143
if (template) {
142144
const file = template.src
143-
? template.src
145+
? getRequireForImport('template', template)
144146
: getTemplateRequire(templateOptions, template);
145147

146148
output += `require('${file}')`;
@@ -229,9 +231,28 @@ export default function(code: string, loaderContext: LoaderContext) {
229231
return `${tModule.query}!./${tModule.module.title}`;
230232
}
231233

234+
function getRequireForImport(type, impt, i = 0, scoped) {
235+
if (type === 'style') {
236+
const styleCompiler = `!!${getStyleLoaders(
237+
impt.attrs,
238+
moduleId,
239+
!!scoped
240+
)}!${impt.src}`;
241+
242+
const tModule = loaderContext.addDependency(styleCompiler);
243+
244+
return `${tModule.query}!./${tModule.module.title}`;
245+
} else if (type === 'script' || type === 'template') {
246+
const tModule = loaderContext.addDependency(impt.src);
247+
248+
return `./${tModule.module.title}`;
249+
}
250+
return '';
251+
}
252+
232253
function getRequire(type, impt, i = 0, scoped) {
233254
if (type === 'style') {
234-
const styleCompiler = `${getStyleLoaders(
255+
const styleCompiler = `!!${getStyleLoaders(
235256
impt.attrs,
236257
moduleId,
237258
!!scoped

0 commit comments

Comments
 (0)