Skip to content

Commit 27aa057

Browse files
authored
Remove document.write usage from vanilla template (codesandbox#1697)
* Remove document.write usage * Move transpilers to typescript * Fix jest test
1 parent 2707979 commit 27aa057

File tree

31 files changed

+131
-103
lines changed

31 files changed

+131
-103
lines changed

packages/app/src/sandbox/eval/presets/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Preset from './index.ts';
22

3-
import Transpiler from '../transpilers';
3+
import Transpiler from '../transpilers/index.ts';
44

55
function createDummyTranspiler(name: string) {
66
const Klass = class Trans extends Transpiler {

packages/app/src/sandbox/eval/presets/parcel/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export default function initialize() {
3737
],
3838
{},
3939
{
40-
htmlDisabled: true,
4140
setup: manager => {
4241
const packageJSON = manager.configurations.package;
4342

packages/app/src/sandbox/eval/presets/parcel/transpilers/html-worker.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import parse from 'posthtml-parser';
2-
import render from 'posthtml-render';
32
import api from 'posthtml/lib/api';
43

54
import isUrl from './is-url';
@@ -154,17 +153,7 @@ self.addEventListener('message', async event => {
154153
return node;
155154
});
156155

157-
const newHTML = render(res);
158-
159-
let compiledCode = `
160-
function setupHTML() {
161-
const HTML = ${JSON.stringify(newHTML)}
162-
document.open('text/html');
163-
document.write(HTML);
164-
document.close();
165-
}
166-
setupHTML();
167-
`;
156+
let compiledCode = ``;
168157

169158
compiledCode += '\n';
170159
compiledCode += 'function loadResources() {';

packages/app/src/sandbox/eval/transpiled-module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { flattenDeep } from 'lodash-es';
32

43
import { actions, dispatch } from 'codesandbox-api';
@@ -84,6 +83,7 @@ export type LoaderContext = {
8483
emitFile: (name: string, content: string, sourceMap: SourceMap) => void;
8584
options: {
8685
context: string;
86+
config?: object;
8787
[key: string]: any;
8888
};
8989
webpack: boolean;
@@ -95,6 +95,7 @@ export type LoaderContext = {
9595
depPath: string,
9696
options?: {
9797
isAbsolute?: boolean;
98+
isEntry?: boolean;
9899
}
99100
) => void;
100101
resolveTranspiledModule: (

packages/app/src/sandbox/eval/transpilers/angular2-template/index.js renamed to packages/app/src/sandbox/eval/transpilers/angular2-template/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
22
import Transpiler from '../';
3-
import { type LoaderContext } from '../../transpiled-module';
3+
import { LoaderContext } from '../../transpiled-module';
44

55
// using: regex, capture groups, and capture group variables.
66
const templateUrlRegex = /templateUrl\s*:(\s*['"`](.*?)['"`]\s*([,}]))/gm;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const DEFAULT_BABEL_CONFIG = {
3030
* Parses the .babelrc if it exists, if it doesn't it will return a default config
3131
*/
3232
export default function getBabelConfig(
33-
config: ?Object,
33+
config: Object | undefined,
3434
loaderOptions: Object,
3535
path: string,
3636
isV7: boolean = false

packages/app/src/sandbox/eval/transpilers/babel/index.js renamed to packages/app/src/sandbox/eval/transpilers/babel/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
// @flow
2+
// @ts-ignore
23
import BabelWorker from 'worker-loader?publicPath=/&name=babel-transpiler.[hash:8].worker.js!./worker/index.js';
34
import { isBabel7 } from '@codesandbox/common/lib/utils/is-babel-7';
45

56
import regexGetRequireStatements from './worker/simple-get-require-statements';
67
import getBabelConfig from './babel-parser';
78
import WorkerTranspiler from '../worker-transpiler';
8-
import { type LoaderContext } from '../../transpiled-module';
9-
import type { default as Manager } from '../../manager';
9+
import { LoaderContext } from '../../transpiled-module';
10+
import Manager from '../../manager';
1011

1112
import delay from '../../../utils/delay';
1213
import { shouldTranspile } from './check';
1314

15+
const global = window as any;
16+
1417
// Right now this is in a worker, but when we're going to allow custom plugins
1518
// we need to move this out of the worker again, because the config needs
1619
// to support custom plugins
@@ -24,16 +27,16 @@ class BabelTranspiler extends WorkerTranspiler {
2427
startupWorkersInitialized = false;
2528

2629
async getWorker() {
27-
while (typeof window.babelworkers === 'undefined') {
30+
while (typeof global.babelworkers === 'undefined') {
2831
await delay(50); // eslint-disable-line
2932
}
3033

31-
if (window.babelworkers.length === 0) {
34+
if (global.babelworkers.length === 0) {
3235
return super.getWorker();
3336
}
3437

3538
// We set these up in startup.js.
36-
return window.babelworkers.pop();
39+
return global.babelworkers.pop();
3740
}
3841

3942
doTranspilation(
@@ -92,7 +95,7 @@ class BabelTranspiler extends WorkerTranspiler {
9295
);
9396

9497
const babelConfig = getBabelConfig(
95-
foundConfig || loaderOptions.config,
98+
foundConfig || (loaderOptions as any).config,
9699
loaderOptions,
97100
path,
98101
isV7
@@ -127,7 +130,7 @@ class BabelTranspiler extends WorkerTranspiler {
127130
});
128131
}
129132

130-
async getTranspilerContext(manager: Manager) {
133+
async getTranspilerContext(manager: Manager): Promise<any> {
131134
return new Promise(async resolve => {
132135
const baseConfig = await super.getTranspilerContext(manager);
133136

@@ -142,6 +145,7 @@ class BabelTranspiler extends WorkerTranspiler {
142145
babelTranspilerOptions,
143146
},
144147
'babelContext',
148+
// @ts-ignore
145149
{},
146150
(err, data) => {
147151
const { version, availablePlugins, availablePresets } = data;

packages/app/src/sandbox/eval/transpilers/base64/index.js renamed to packages/app/src/sandbox/eval/transpilers/base64/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// @flow
2-
import Transpiler from '../';
3-
import { type LoaderContext } from '../../transpiled-module';
2+
import Transpiler, { TranspilerResult } from '../';
43

54
import mimes from './mimes.json';
65

@@ -24,9 +23,9 @@ class Base64Transpiler extends Transpiler {
2423
}
2524

2625
doTranspilation(code: string) {
27-
return new Promise(resolve => {
26+
return new Promise<TranspilerResult>(resolve => {
2827
const reader = new FileReader();
29-
// $FlowIssue
28+
// @ts-ignore
3029
reader.readAsDataURL(code);
3130

3231
reader.onloadend = function() {

packages/app/src/sandbox/eval/transpilers/binary/index.js renamed to packages/app/src/sandbox/eval/transpilers/binary/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
2-
import Transpiler from '../';
3-
import { type LoaderContext } from '../../transpiled-module';
2+
import Transpiler, { TranspilerResult } from '../';
3+
import { LoaderContext } from '../../transpiled-module';
44

55
/**
66
* Just fetches a file from the interwebs and converts it to a blob
@@ -13,7 +13,10 @@ class BinaryTranspiler extends Transpiler {
1313
super('binary-loader');
1414
}
1515

16-
doTranspilation(code: string, loaderContext: LoaderContext) {
16+
doTranspilation(
17+
code: string,
18+
loaderContext: LoaderContext
19+
): Promise<TranspilerResult> {
1720
return fetch(code)
1821
.then(res => res.blob())
1922
.then(blob => ({ transpiledCode: blob }));

packages/app/src/sandbox/eval/transpilers/coffee/index.js renamed to packages/app/src/sandbox/eval/transpilers/coffee/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
// @flow
1+
// @ts-ignore
22
import CoffeeWorker from 'worker-loader?publicPath=/&name=coffee-transpiler.[hash:8].worker.js!./coffee-worker.js';
33

44
import WorkerTranspiler from '../worker-transpiler';
5-
import { type LoaderContext } from '../../transpiled-module';
5+
import { LoaderContext } from '../../transpiled-module';
6+
import { TranspilerResult } from '..';
67

78
class CoffeeTranspiler extends WorkerTranspiler {
89
worker: Worker;
@@ -13,7 +14,10 @@ class CoffeeTranspiler extends WorkerTranspiler {
1314
this.cacheable = false;
1415
}
1516

16-
doTranspilation(code: string, loaderContext: LoaderContext) {
17+
doTranspilation(
18+
code: string,
19+
loaderContext: LoaderContext
20+
): Promise<TranspilerResult> {
1721
return new Promise((resolve, reject) => {
1822
const path = loaderContext.path;
1923

0 commit comments

Comments
 (0)